<?php
namespace app\gateway\controller;

use think\facade\Config;
use Workerman\Worker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use GatewayWorker\BusinessWorker;

class Startgatewaylinux
{
    /**
     * 构造函数
     * @access public
     */
    public function __construct(){

        //gateway配置参数
        $gateway_config = Config::get('gateway.');
        //初始化各个GatewayWorker
        //初始化register register 服务必须是text协议
        $register = new Register($gateway_config['register_text']);
        //初始化 bussinessWorker 进程
        $worker = new BusinessWorker();
        // worker名称
        $worker->name = $gateway_config['worker_name'];
        // bussinessWorker进程数量
        $worker->count = $gateway_config['business_process_count'];
        // 服务注册地址
        $worker->registerAddress = $gateway_config['register_address'];
        //设置处理业务的类,此处制定Events的命名空间
        $worker->eventHandler = 'app\gateway\controller\Events';
        // 初始化 gateway 进程
        $gateway = new Gateway($gateway_config['websocket_domain']);
        // 设置名称，方便status时查看
        $gateway->name = $gateway_config['gateway_name'];
        // 设置进程数，gateway进程数建议与cpu核数相同
        $gateway->count =  $gateway_config['gateway_process_count'];
        // 分布式部署时请设置成内网ip（非127.0.0.1）
        $gateway->lanIp = $gateway_config['lanIp'];
        // 内部通讯起始端口，假如$gateway->count=4，起始端口为4000
        // 则一般会使用4000 4001 4002 4003 4个端口作为内部通讯端口
        $gateway->startPort = $gateway_config['gateway_startPort'];
        // 心跳间隔
        $gateway->pingInterval = $gateway_config['pingInterval'];
        // 心跳数据
        $gateway->pingData = $gateway_config['ping_data'];
        // 服务注册地址
        $gateway->registerAddress = $gateway_config['register_address'];

        //运行所有Worker;
        Worker::runAll();
    }
}